package com.zeyi.blog.controller;


import com.zeyi.blog.dto.Result;
import com.zeyi.blog.dto.StatusCode;
import com.zeyi.blog.entity.Dictionary;
import com.zeyi.blog.service.DictionaryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 字典表(Dictionary)表控制层
 *
 * @author 黎先森
 * @since 2020-07-23 08:20:52
 */
@RestController
@RequestMapping("dictionary")
public class DictionaryController {
    /**
     * 服务对象
     */
    @Resource
    private DictionaryService dictionaryService;

    /**
     * 查询所有数据(转化树形)
     *
     * @return 所有数据
     */
    @GetMapping
    public Result selectAll() {
        return new Result(true, StatusCode.OK, "查询成功", this.dictionaryService.getAll());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return new Result(true, StatusCode.OK, "查询成功", dictionaryService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dictionary 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody Dictionary dictionary) {
        return new Result(true, StatusCode.OK, "添加成功", this.dictionaryService.save(dictionary));
    }

    /**
     * 修改数据
     *
     * @param dictionary 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody Dictionary dictionary) {
        return new Result(true, StatusCode.OK, "修改成功", this.dictionaryService.updateById(dictionary));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestBody List<String> idList) {
        return new Result(true, StatusCode.OK, "删除成功", this.dictionaryService.removeByIds(idList));
    }


    /**
     * 通过组名查询（获得List）
     */
    @GetMapping("/group")
    public Result findAllByGroup(@RequestParam(value = "groupName", required = true) String groupName){
        return new Result(true, StatusCode.OK,"查询成功", dictionaryService.getAllByGroup(groupName));
    }
}